<?php

namespace app\common\model;

use think\Model;
use traits\model\SoftDelete;

class JoinApplication extends Model
{

    use SoftDelete;

    //数据库
    protected $connection = 'database';
    // 表名
    protected $name = 'join_application';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'type_text',
        'tm_type_text',
        'tm_reg_valid_time_text',
        'tm_auth_valid_time_text',
        'status_text',
        'entity_name',
        'entity_bank_account',
    ];
    

    
    public function getTypeList()
    {
        return ['0' => __('Type 0'), '1' => __('Type 1')];
    }

    public function getTmTypeList()
    {
        return ['0' => __('Tm_type 0'), '1' => __('Tm_type 1')];
    }

    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1'), '2' => __('Status 2')];
    }


    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getTmTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['tm_type']) ? $data['tm_type'] : '');
        $list = $this->getTmTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getTmRegValidTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['tm_reg_valid_time']) ? $data['tm_reg_valid_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getTmAuthValidTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['tm_auth_valid_time']) ? $data['tm_auth_valid_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setTmRegValidTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setTmAuthValidTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    public function getEntityNameAttr($value, $data)
    {
        $type = isset($data['type']) ? $data['type'] : 0;
        if ($type == 0) {
            return isset($data['ind_name']) ? $data['ind_name'] : '';
        } else {
            return isset($data['ent_name']) ? $data['ent_name'] : '';
        }
    }

    public function getEntityBankAccountAttr($value, $data)
    {
        $type = isset($data['type']) ? $data['type'] : 0;
        if ($type == 0) {
            return isset($data['ind_bank_account']) ? $data['ind_bank_account'] : '';
        } else {
            return isset($data['ent_bank_account']) ? $data['ent_bank_account'] : '';
        }
    }
}
